package com.LeeCode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 合并K个升序链表
 */

public class Code023 {
    public static void main(String[] args) {

    }

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode dummyHead = new ListNode(), cur = dummyHead;
        PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val);
        for (ListNode node : lists) {
            if (node != null)
                pq.offer(node);
        }

        while (!pq.isEmpty()) {
            ListNode node = pq.poll();
            cur.next = node;
            cur = cur.next;
            if (node.next != null) {
                pq.offer(node.next);
            }
        }

        return dummyHead.next;
    }
}
